from rest_framework import serializers
from .models import Students, Classes


# class StudentsSerializer(serializers.Serializer):
#     # 创建序列化器
#     id = serializers.IntegerField(read_only=True, label='ID')
#     name = serializers.CharField(max_length=10, label='姓名')
#     age = serializers.IntegerField(required=False, label='年龄')
#     sex = serializers.IntegerField(required=False, label='性别')
#     create_time = serializers.DateTimeField(read_only=True, label='创建时间')
#     update_time = serializers.DateTimeField(required=False, label='修改时间')
#     is_delete = serializers.BooleanField(required=False, label='逻辑删除', default=False)
#
#     def create(self, validated_data):
#         return Students.objects.create(**validated_data)
#
#     def update(self, instance, validated_data):
#         instance.name = validated_data.get('name', instance.name)
#         instance.age = validated_data.get('age', instance.age)
#         instance.sex = validated_data.get('sex', instance.sex)
#         instance.save()
#         return instance


# 应用与班级模型中进行展示学生消息
class StudentsSerializerSimple(serializers.ModelSerializer):

    class Meta:
        model = Students
        exclude = ['create_time', 'update_time', 'is_delete', 'classes']


# 应用与学生模型中进行展示班级消息
class ClassesSerializerSimple(serializers.ModelSerializer):

    class Meta:
        model = Classes
        exclude = ['create_time', 'update_time', 'is_delete']


# 继承更加强大的类，可以节省冗余代码
class StudentsSerializer(serializers.ModelSerializer):
    classname = serializers.CharField(source='classes.name', read_only=True, label='班级名称')
    # 这句代码很鸡肋,因为外键就在自己表上，不需要去套了
    # classes = ClassesSerializerSimple(read_only=True, label='班级')

    class Meta:
        model = Students
        exclude = ['create_time', 'update_time', 'is_delete']
        extra_kwargs = {
            'classes': {'write_only': True},
        }

    def validate_age(self, value):
        if value < 20 or value > 30:
            raise serializers.ValidationError('年龄要在20-30之间')
        return value

    # def validate(self, attrs):
    #     name = attrs.get('name')
    #     age = attrs.get('age')
    #     if 20 < age < 30:
    #         raise serializers.ValidationError('年龄在20-30之间')
    #     if '七' not in name:
    #         raise serializers.ValidationError('名称必须包含七')
    #     return attrs


# 班级序列化器
class ClassesSerializer(serializers.ModelSerializer):
    students_set = StudentsSerializerSimple(many=True, read_only=True, label='学生列表')
    # stu_name = serializers.CharField(source='students.name', read_only=True, label='学生名称')

    class Meta:
        model = Classes
        exclude = ['create_time', 'update_time', 'is_delete']


# 被逻辑删除学生序列化器
class IsDeleteStudentsSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True, label='ID')
    name = serializers.CharField(read_only=True, max_length=10, label='姓名')
    age = serializers.IntegerField(read_only=True, label='年龄')
    sex = serializers.IntegerField(read_only=True, label='性别')
    create_time = serializers.DateTimeField(read_only=True, label='创建时间')
    update_time = serializers.DateTimeField(required=False, label='修改时间')
    is_delete = serializers.BooleanField(required=False, label='逻辑删除', default=True)
    classname = serializers.CharField(source='classes.name', read_only=True, label='班级名称')

    # 重写修改函数
    def update(self, instance, validated_data):
        # 进行修改并保存
        instance.is_delete = validated_data.get('is_delete', instance.is_delete)
        instance.save()
        return instance


# 被逻辑删除班级序列化器
class IsDeleteClassesSerializer(serializers.Serializer):
    id = serializers.IntegerField(read_only=True, label='ID')
    name = serializers.CharField(read_only=True, max_length=25, label='班级名称')
    slogan = serializers.CharField(read_only=True, max_length=30, label='班级口号')
    create_time = serializers.DateTimeField(read_only=True, label='创建时间')
    update_time = serializers.DateTimeField(required=False, label='修改时间')
    is_delete = serializers.BooleanField(required=False, label='逻辑删除', default=True)

    # 返回班级的学生信息
    students_set = StudentsSerializerSimple(many=True, read_only=True, label='学生列表')

    # 重写修改函数
    def update(self, instance, validated_data):
        # 进行修改并保存
        instance.is_delete = validated_data.get('is_delete', instance.is_delete)
        instance.save()
        return instance
